package controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import serice.impl.MyCartserivceipml;
import entity.LayuiEntity;
import entity.MyCart;
import entity.ajaxResult;


@Controller
public class MyCartcontroller {
	@Autowired
	private MyCartserivceipml service;
	@RequestMapping("inserMyCart")
	@ResponseBody
	public ajaxResult inserMyCart(MyCart cart){
		ajaxResult ajax=new ajaxResult();
		try {
			service.insertMyCart(cart);
			ajax.setMsg("success");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			ajax.setMsg("fail");
		}
		return ajax;
	}
	@RequestMapping("showMyCart")
	@ResponseBody
	public LayuiEntity<MyCart> showMyCart(Integer uid,@RequestParam(defaultValue="1")Integer page){
		Integer start=(page-1)*5;
		Integer offset=5;
		List<MyCart> list=service.selectmycart(uid, start, offset);
		Integer count=service.getCount(uid);
		LayuiEntity<MyCart> entity =new LayuiEntity<MyCart>();
		entity.setCount(count);
		entity.setData(list);
		return entity;
	}
	@RequestMapping("checkoutCart")
	@ResponseBody
	public String checkoutCart(Integer uid){
		try {
			service.checkOutCart(uid);
			return"success";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "fail";
		}
	}
	@RequestMapping("getCartTotal")
	@ResponseBody
	public String getCartTotal(Integer uid){
		return service.getCartTotalPrice(uid)+"";
	}
}
